﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class Message : System.Web.UI.Page
	{
		private static IEngine engine = Engine.Instance();

		String visitorName;

		List<IUser> userList;

		protected void Page_Load( object sender, EventArgs e )
		{
			visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			else
			{
				if( Request.QueryString["msg"] != null )
				{
					if( Request.QueryString["msg"].Equals( "1" ) ) MessageLabel.Text = "A műveletet sikeresen végrehajtottuk!";
					else if( Request.QueryString["msg"].Equals( "0" ) ) MessageLabel.Text = "Hiba történt a művelet végrehajtása közben";
				}

				if( !Page.IsPostBack )
				{
					RadioButtonList1.SelectedIndex = 0;
					RadioButtonList2.SelectedIndex = 0;
				}

				try
				{
					userList = engine.GetUsers();

					foreach( IUser u in userList )
					{
						DropDownList1.Items.Add( u.Name.Trim() + " - " + u.Neptun.Trim() );
					}
				}
				catch( EngineException ex )
				{
					Response.Redirect( "~/Admin/Message.aspx?msg=0" );
				}
			}
		}

		protected void Button1_Click( object sender, EventArgs e )
		{
			try
			{
				string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd( '/' ) + '/';
				engine.SetProgramPage( baseUrl );

				IUser address = null;
				bool isBroadCast = false;
				bool teacherOnly = false;
				bool adminOnly = false;

				if( RadioButtonList2.SelectedIndex == 0 )
				{
					isBroadCast = true;
				}
				else if( RadioButtonList2.SelectedIndex == 1 )
				{
					isBroadCast = true;
					teacherOnly = true;
				}
				else if( RadioButtonList2.SelectedIndex == 2 )
				{
					isBroadCast = true;
					adminOnly = true;
				}
				else
				{
					address = userList[DropDownList1.SelectedIndex];
				}


				if( RadioButtonList1.SelectedIndex < 2 )
				{
					teacherOnly = true;
					if( address != null )
					{
						if( !engine.IsTeacher( address.Neptun ) )
						{
							address = null;
						}
					}
				}

				if( RadioButtonList1.SelectedIndex < 2 )
				{

					MailEngine.DefaultMessage msg;


					if( isBroadCast == true )
					{
						if( RadioButtonList1.SelectedIndex == 0 )
						{
							msg = MailEngine.DefaultMessage.setPreferences;
						}
						else
						{
							msg = MailEngine.DefaultMessage.seeTimeTable;
						}
						engine.SendBroadcastNotification( msg, teacherOnly, adminOnly );
					}
					else
					{
						if( address != null )
						{
							ITeacher t = engine.GetTeacherProfile( address.Neptun );
							if( RadioButtonList1.SelectedIndex == 0 ) engine.SendPreferenceMessage( t );
							else engine.SendTimeTable( t );
						}
					}
				}
				else
				{
					if( isBroadCast == true )
					{
						engine.SendBroadcastNotification( TextBox1.Text, teacherOnly, adminOnly );
					}
					else
					{
						engine.SendNotification( address, TextBox1.Text );
					}
				}

				Response.Redirect( "~/Admin/Message.aspx?msg=1" );
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/Message.aspx?msg=0" );
			}
		}
	}
}